{extend name="public:base"}
{block name="main"}
<div class="layui-form">
  条件搜索：
  <div class="layui-inline">
    <input class="layui-input" name="start" id="start"  placeholder="请输入开始时间">
  </div>
  <div class="layui-inline">
    <input class="layui-input" name="end" id="end"  placeholder="请输入结束时间">
  </div>
  <div class="layui-inline">
    <input class="layui-input" name="key"  placeholder="请输入搜索关键字">
  </div>

  <button class="layui-btn" data-type="reload" lay-filter="submit" lay-submit>搜索</button>
  <button class="layui-btn layui-btn-primary" type="reset" data-type="reload" lay-filter="reset" lay-submit>重置</button>

</div>
<table class="layui-table" lay-data="{height:500,url:location.href, page:true, id:'table'}" lay-filter="table">
  <thead>
    <tr>
      <th lay-data="{field:'roleid',  sort: true}">用户ID</th>
      <th lay-data="{field:'px',sort:true}">排序</th>
      <th lay-data="{field:'name'}">角色名称</th>
      <th lay-data="{field:'status',templet:'#status'}">状态</th>
      <th lay-data="{field:'addtime', sort: true}">添加时间</th>
      <th lay-data="{field:'uptime', sort: true}">修改时间</th>
      <th lay-data="{fixed: 'right', align:'center', toolbar: '#barDemo'}">操作</th>
    </tr>
  </thead>
</table>

<script type="text/html" id="barDemo">
  <div class="layui-btn  layui-btn-sm" lay-event="edit">编辑</div>
  <div class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</div>
</script>


<script>
layui.use(['table','laydate','form'], function(){
  var table = layui.table;
  var laydate = layui.laydate;
  var form = layui.form;
    laydate.render({
      elem: '#start',
      max: 0
    });
    laydate.render({
      elem: '#end',
      max: 1
   });
   
  //搜索
  form.on('submit(submit)', function(data){

    var start=$("input[name='start']").val();
    var end=$("input[name='end']").val();
    var key=$("input[name='key']").val();

    table.reload('table', {
      where: { 
        start:start,
        end:end,
        key:key
      }
    });

    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
  });

  //重置
  form.on('submit(reset)', function(data){

    location.reload();
    return false; 

  });


  table.on('tool(table)', function(obj){
    return false;
    var data = obj.data;

    if(obj.event === 'del'){
      layer.confirm('真的删除本条数据吗？', function(index){

        $.post("{:Url('roledel')}",{'roleid': data.roleid},function(data){
           if(data=='1'){
              alert("操作成功~");
              obj.del();
              layer.close(index);
           }else{
             alert("删除失败请重试！");
           }
        })
        
      });
    } else if(obj.event === 'edit'){
        location.href="info/roleid/"+data.roleid;
    
    }
  });

  //图片查看
  layer.photos({
    photos: 'div'
    ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
  });
  
  //开关
  form.on('switch(status)', function(data){

    $.post("{:url('roleup')}",{'status':data.elem.checked,'roleid':data.value},function(res){
      if(res=='0'){
        alert("操作失败");
      }
    });
   
  });  

});

</script>


<script type="text/html" id="status">
  {{#  if(d.status == 1){ }}
    <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" value="{{d.roleid}}" checked>
  {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" lay-filter="status"  value="{{d.roleid}}">
  {{#  } }}
</script>

{/block}